﻿using Microsoft.AspNetCore.SignalR.Client;


var id = Guid.NewGuid().ToString();

var connection = new HubConnectionBuilder()
            .WithUrl("http://localhost:5227/messaging")
            .Build();

await connection.StartAsync();


Console.WriteLine("Starting connection. Press Ctrl-C to close.");
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (sender, a) =>
{
    a.Cancel = true;
    cts.Cancel();
};

connection.Closed += e =>
{
    Console.WriteLine("Connection closed with error: {0}", e);

    cts.Cancel();
    return Task.CompletedTask;
};


connection.On("SendMessage1", (string args) =>
{
    Console.WriteLine(DateTime.Now + "Market opened" + args);
});

connection.On("SendProcessStepsAsync", (List<string> connectionIds) =>
{
    Console.WriteLine("Market opened" + args);
});

//connection.On("SendMessage3", (string message, Test test) =>
//{
//    Console.WriteLine("Market opened" + args);
//});

connection.On("marketClosed", (string args1) =>
{
    Console.WriteLine("Market closed" + args1);
});

connection.On("marketReset", () =>
{
    // We don't care if the market rest
});

//var channel = await connection.StreamAsChannelAsync<bool>("SendMessage", CancellationToken.None);
//while (await channel.WaitToReadAsync() && !cts.IsCancellationRequested)
//{
//    while (channel.TryRead(out var stock))
//    {
//        Console.WriteLine($"{stock}");
//    }
//}

//List<Task<bool>> l = new();

//for (int i = 0; i < 1000; i++)
//{
//    //   Console.WriteLine($"calling send message {i}");
//    l.Add(connection.InvokeAsync<bool>("SendMessage2222", $"{id}  --- call {i}"));
//}

//Console.WriteLine($"all invoked");
//await Task.WhenAll(l);

//Console.WriteLine(string.Join(Environment.NewLine, l.Select(t => t.Result)));

Console.WriteLine($"End");

Console.ReadLine();


